/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Net.Mail;
using System.Windows.Forms;
using SurveillanceDotNet.Model.RegularActions;
using SurveillanceDotNet.Model;

namespace SurveillanceDotNet.RegularActions
{
    public partial class FtpRegularActionForm : Form
    {
        public FTPPictureUploadRegularAction Action { get; set; }

        private Database _Database;

        public FtpRegularActionForm()
        {
            InitializeComponent();

            _Database = Database.Instance;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (tbxName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the action!", "Missing name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            double interval;

            if (!double.TryParse(tbxInterval.Text, out interval))
            {
                MessageBox.Show("Please enter a valid number as interval!", "Missing EMail account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (tbxFtpUrl.Text.Length == 0) {
                MessageBox.Show("Please enter a valid ftp address!", "Invalid ftp address", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (Action == null)
            {
                Action = new FTPPictureUploadRegularAction(tbxFtpUrl.Text);
            }

            Action.UseAuthentication = chxUseAuthentication.Checked;
            Action.UserName = tbxUserName.Text;
            Action.Password = tbxPassword.Text;
            Action.FtpUrl = tbxFtpUrl.Text;

            Action.Name = tbxName.Text;
            Action.Interval = interval;

            this.DialogResult = DialogResult.OK;
        }

        private void EMailRegularActionForm_Load(object sender, EventArgs e)
        {
            tbxUserName.Visible = false;
            lblUserName.Visible = false;
            tbxPassword.Visible = false;
            lblPassword.Visible = false;

            if (Action != null)
            {
                tbxName.Text = Action.Name;
                tbxInterval.Text = Action.Interval.ToString();

                tbxFtpUrl.Text = Action.FtpUrl;

                chxUseAuthentication.Checked = Action.UseAuthentication;
                tbxUserName.Text = Action.UserName;
                tbxPassword.Text = Action.Password;
            }
            else
            {
                int exitingActionCounter = 1;

                foreach (RegularAction action in _Database.RegularActions)
                {
                    if (action is FTPPictureUploadRegularAction) exitingActionCounter++;
                }

                tbxName.Text = "Upload Picture " + exitingActionCounter;
            }
        }

        private void chxUseAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            tbxUserName.Visible = chxUseAuthentication.Checked;
           lblUserName.Visible = chxUseAuthentication.Checked;
            tbxPassword.Visible = chxUseAuthentication.Checked;
            lblPassword.Visible = chxUseAuthentication.Checked;
        }
    }
}
